﻿//------------------------------------------------------------------------------------------------- 
// <copyright file="ExceptionResources.cs" company="Oliver Hallam">
// Copyright (c) 2008 Oliver Hallam
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// </copyright>
//-------------------------------------------------------------------------------------------------

namespace DBLite
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Globalization;
    using System.Resources;
    
    /// <summary>
    /// A class that provides exception messages.
    /// </summary>
    internal static class ExceptionResources
    {
        #region Fields

        /// <summary>
        /// The resource manager managing the message resources.
        /// </summary>
        private static ResourceManager resourceManager;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the cached ResourceManager instance used by this class.
        /// </summary>
        /// <value>The resource manager.</value>
        internal static ResourceManager ResourceManager
        {
            get
            {
                Contract.Ensures(Contract.Result<ResourceManager>() != null);

                if (ReferenceEquals(resourceManager, null))
                {
                    resourceManager = new ResourceManager("DBLite.Exceptions", typeof(ExceptionResources).Assembly);
                }

                return resourceManager;
            }
        }

        #endregion

        #region Static Methods

        /// <summary>
        /// Gets the message to be used in an <see cref="ArgumentNullException"/>.
        /// </summary>
        /// <param name="argumentName">Name of the argument.</param>
        /// <returns>A message to be used in an <see cref="ArgumentNullException"/>.</returns>
        public static string ArgumentNull(string argumentName)
        {
            Contract.Requires(argumentName != null);
            Contract.Ensures(Contract.Result<string>() != null);

            var message = string.Format(CultureInfo.CurrentUICulture, GetString("ArgumentNull"), argumentName);

            return message;
        }

        /// <summary>
        /// Gets the message to be used in an <see cref="ArgumentOutOfRangeException"/>.
        /// </summary>
        /// <param name="argumentName">Name of the argument.</param>
        /// <returns>The message to be used in an <see cref="ArgumentOutOfRangeException"/>.</returns>
        public static string ArgumentOutOfRange(string argumentName)
        {
            Contract.Requires(argumentName != null);
            Contract.Ensures(Contract.Result<string>() != null);

            return string.Format(CultureInfo.CurrentUICulture, GetString("ArgumentOutOfRange"), argumentName);
        }

        /// <summary>
        /// Gets the message to be used to report an invalid column ID.
        /// </summary>
        /// <param name="columnId">The invalid column ID.</param>
        /// <returns>
        /// The message to be used to report an invalid column ID.
        /// </returns>
        public static string InvalidColumnId(int columnId)
        {
            Contract.Ensures(Contract.Result<string>() != null);

            return string.Format(CultureInfo.CurrentUICulture, GetString("InvalidColumnId"), columnId);
        }

        /// <summary>
        /// Gets the message to be used to report an invalid row ID.
        /// </summary>
        /// <param name="rowId">The invalid row ID.</param>
        /// <returns>
        /// The message to be used to report an invalid row ID.
        /// </returns>
        public static string InvalidRowId(int rowId)
        {
            Contract.Ensures(Contract.Result<string>() != null);

            return string.Format(CultureInfo.CurrentUICulture, GetString("InvalidRowId"), rowId);
        }

        /// <summary>
        /// Gets the message to be used to report a value that should not be <see langword="null"/>.
        /// </summary>
        /// <param name="valueName">Name of the value.</param>
        /// <returns>A message to be used to report a value that should not be <see langword="null"/>.</returns>
        public static string ValueNull(string valueName)
        {
            Contract.Requires(valueName != null);
            Contract.Ensures(Contract.Result<string>() != null);

            return string.Format(CultureInfo.CurrentUICulture, GetString("ValueNull"), valueName);
        }

        /// <summary>
        /// Gets the message to be used to report a value that should not be <see langword="null"/>.
        /// </summary>
        /// <returns>A message to be used to report a value that should not be <see langword="null"/>.</returns>
        public static string ColumnNull()
        {
            Contract.Ensures(Contract.Result<string>() != null);

            return GetString("ColumnNull");
        }

        /// <summary>
        /// Gets a string from the resource manager.
        /// </summary>
        /// <param name="identifier">The identifier of the resource.</param>
        /// <returns>The identified string resource.</returns>
        private static string GetString(string identifier)
        {
            Contract.Requires(identifier != null);
            Contract.Ensures(Contract.Result<string>() != null);

            var message = ResourceManager.GetString(identifier, CultureInfo.CurrentUICulture);
            return message ?? "Message not found";
        }

        #endregion
    }
}